package com.jeespring.elasticsearch.controller;

import com.github.pagehelper.PageInfo;
import com.jeespring.elasticsearch.dto.UserIndexDTO;
import com.jeespring.elasticsearch.entity.User;
import com.jeespring.elasticsearch.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.index.query.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @description UserController
 * @author 张朝阳
 * @date 2021/2/15 23:10
 */
@RestController
@RequestMapping("/user")
public class UserController {


    @Autowired
    private ElasticsearchRestTemplate elasticTemplate;


    @Autowired
    private UserService userService;

    @RequestMapping("/selectAllByPage")
    public PageInfo selectAllByPage(@RequestParam("pageNumber") int pageNumber,
                                    @RequestParam("pageSize") int pageSize) {
        User user = new User();
        return userService.selectAllByPage(user, pageNumber, pageSize);
    }


    @RequestMapping("/syncAll")
    public void syncAll() {
        userService.syncAll();
    }


    /**
     * 多字段匹配
     */
    @RequestMapping("/multiMatch")
    public Object multiMatch(String key, @PageableDefault(sort = "userCode", direction = Sort.Direction.DESC) Pageable pageable) {
        NativeSearchQuery nativeSearchQuery =
                new NativeSearchQueryBuilder().withQuery(QueryBuilders.multiMatchQuery(key, "userName", "userNickName")).withPageable(pageable).build();
        SearchHits<UserIndexDTO> searchHits = elasticTemplate.search(nativeSearchQuery, UserIndexDTO.class);
        searchHits.getSearchHits().forEach(System.out::println);
        return searchHits.getSearchHits();
    }

    /**
     * 多字段匹配
     */
    @RequestMapping("/boolMatch")
    public Object boolMatch(String key, String key2, @PageableDefault(sort = "userCode", direction = Sort.Direction.DESC) Pageable pageable) {
        BoolQueryBuilder catronBoolquery = QueryBuilders.boolQuery();
        if (!StringUtils.isEmpty(key)) {
            MultiMatchQueryBuilder multiMatchQueryBuilder = QueryBuilders.multiMatchQuery(key, "userName", "userNickName");
            multiMatchQueryBuilder.operator(Operator.AND);//默认是"or"，如果修改成"and",则表示记录中要同时包含条件
            MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("topOrgCode", key2);
            catronBoolquery.must(multiMatchQueryBuilder);//将第一个条件插入到must
            catronBoolquery.must(matchQueryBuilder);//将第二个根据时间筛选的条件插入到must
        }
        System.out.println(catronBoolquery);//打印查询语句
        NativeSearchQuery nativeSearchQuery = new NativeSearchQueryBuilder().withQuery(catronBoolquery).build();
        SearchHits<UserIndexDTO> searchHits = elasticTemplate.search(nativeSearchQuery, UserIndexDTO.class);
        searchHits.getSearchHits().forEach(System.out::println);
        return searchHits.getSearchHits();
    }
}
